package com.woniuxy.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.AssociateDao;
import com.woniuxy.entitys.Associate;
import com.woniuxy.entitys.Client;
import com.woniuxy.services.AssociateService;
import com.woniuxy.services.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class AssociateServiceImpl implements AssociateService {

    @Autowired
    private AssociateDao associateDao;
    @Autowired
    private ClientService clientService;


    /*
    * 根据客户id获得交往记录：
    * 1：根据公司id获得所有联系人集合；
    * 2：根据联系人集合获取所有的交往记录集合
    * */
    @Override
    public PageInfo<Associate> getAssByComId(String selMsg, String comid, Integer currentPage, Integer pageSize) {
        //获取所有联系人人的id集合
//        ClientServiceImpl clientService = new ClientServiceImpl();
        List<Client> clientList = clientService.selAllByComId(comid);
        List<Integer> cliIdList = new ArrayList<Integer>();
        for(int i = 0; i < clientList.size(); i++){
            cliIdList.add(clientList.get(i).getCli_id());
        }
        //根据联系人集合获得相应的交往记录
        PageHelper.startPage(currentPage,pageSize);
        List<Associate> assList = associateDao.getAssByComIds(selMsg,cliIdList);
        //将结果封装到pageinfo中
        PageInfo<Associate> pageInfo = new PageInfo(assList);
        return pageInfo;
    }

    //删除交往记录：更改status为0
    @Override
    public Integer delAss(Associate associate) {
        associate.setAss_status(0);
        int res = associateDao.updateByPrimaryKeySelective(associate);
        return res;
    }

    @Override
    public Integer addAss(Associate associate) {
        return associateDao.insertSelective(associate);
    }
}
